package com.zs4sdk.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.zs4sdk.Zs4GameBase;
import com.zs4sdk.common.Extra;
import com.zs4sdk.extra.Zs4GameUserInfo;
import com.common.library.util.EncryptHelper;
import com.common.library.util.FileUtils;
import com.common.library.util.Helper;
import com.common.library.util.PreferencesHelper;

/**
 * 
 * function: 数据
 * 
 * @ author: aXiBa
 */
public class Zs4GameData {
	
	private static final String TAG = Zs4GameData.class.getSimpleName();
	
	private static Zs4GameData sInstance;

	// app信息
	private int appId;
	private String appKey;
	private String channel;

    /**微信分享AppId*/
    private String wechatAppId;
	
	// 屏幕方向
	private int screen_orientation = Zs4GameBase.SCREEN_ORIENTATION_LANDSCAPE;
	
	// 包名
	private static String package_name = "";

	private  int countKefuMessage = 0;//客服消息

	private int loginFirstTime = 1;//是否第一次登陆
	
	// 友盟key
//	private static String umengKey = "";
	
	// 是否为调试模式
	private boolean isDebug = false;
	
	// 是否显示welcomeToast(默认显示)
	private boolean isShowWelcomeToastForCustom = true;
	
	// 用户信息
//	private LoginJSONResultEntity loginJSONResultEntity;   // 登录的用户信息
	private LoginJSONResultEntity loginJSONResultEntity_1700;   // 登录的用户信息,1.7.0.0以上，区别1.6

	// 程序监听参数
	private boolean isUpdateMenuing = false;   // 是否正在更新更新

	private boolean menuLeft = true; //悬浮窗初始位置是否在左边
	
	// 支付参数
	private String userId;     //角色id
	private String playerId;     //角色id
	private String playerName;   //角色名字
	private String playerLevel;   //角色等级
	private String servId;     //服务器id
	private String servName;   //服务器名称
	private String payExpandData; //支付的额外参数
	
	// 是否读取渠道ID
	private boolean isReadChannel = false;
	private String channelID = "";   // 渠道id

	private long kefuTime = 1464146650;//最后一次提交客服问题成功的时间戳
	
	// 是否隐藏切换账号
	private boolean isHiddenSwitchAccount = false;
	
	// 是否显示日志
	private boolean isShowLog = false;
	
	// 悬浮菜单初始化停放位置
	private int initDuckPositionForLogoButton = Zs4GameBase.DUCKPOSITION_LEFTBOTTOM;
	
	// 厂商是否显示悬浮菜单
	private boolean isShowLogoButon = true;
	
	// 是否可以显示欢迎信息
	private boolean isCanShowWelcomeToast = false;

     //公司代号
	private int company;

	//是否设置过密码
	private String isPassword = Extra.JSONReturnResult.HAD_SET_PWD;//0表示没有设置过 1表示设置过了

	public int getCompany() {
		return company;
	}

	public void setCompany(int company) {
		this.company = company;
	}

	public void setAppId(int appId){
		this.appId = appId;
	}

	public int getAppId(){
		return this.appId;
	}

    public void setWechatAppId(String wechatAppId) {
        this.wechatAppId = wechatAppId;
    }

    public String getWechatAppId() {
        return this.wechatAppId;
    }

	public void setAppKey(String appKey){
		this.appKey = appKey;
	}

	public String getAppKey(){
		return this.appKey;
	}
	
	public int getInitDuckPositionForLogoButton() {
		return initDuckPositionForLogoButton;
	}

	public void setInitDuckPositionForLogoButton(int initDuckPositionForLogoButton) {
		this.initDuckPositionForLogoButton = initDuckPositionForLogoButton;
	}

	public boolean isMenuLeft() {
		return menuLeft;
	}

	public void setMenuLeft(boolean menuLeft) {
		this.menuLeft = menuLeft;
	}

	public boolean isShowLogoButon() {
		return isShowLogoButon;
	}

	public void setShowLogoButon(boolean isShowLogoButon) {
		this.isShowLogoButon = isShowLogoButon;
	}
	
	public boolean isCanShowWelcomeToast() {
		return isCanShowWelcomeToast;
	}

	public void setCanShowWelcomeToast(boolean isCanShowWelcomeToast) {
		this.isCanShowWelcomeToast = isCanShowWelcomeToast;
	}

	public boolean isShowWelcomeToastForCustom() {
		return isShowWelcomeToastForCustom;
	}

	public void setShowWelcomeToastForCustom(boolean isShowWelcomeToastForCustom) {
		this.isShowWelcomeToastForCustom = isShowWelcomeToastForCustom;
	}

	public long getKefuTime() {
		return kefuTime;
	}

	public void setKefuTime(long kefuTime) {
		this.kefuTime = kefuTime;
	}

	public String getIsPassword() {
		return isPassword;
	}

	public boolean isPassword(){
		if(loginJSONResultEntity_1700.getIspwd().equals(Extra.JSONReturnResult.HAD_SET_PWD)){
			return true;
		}else return false;
	}


	public void setIsPassword(String isPassword) {
		loginJSONResultEntity_1700.setIspwd(isPassword);
	}

	/**
	 * 
	 * function: 得到屏幕方向
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-3-6 上午9:25:39
	 */
	public int getScreen_orientation() {
		return screen_orientation;
	}
	
	public LoginJSONResultEntity getLoginJSONResultEntity() {
		return loginJSONResultEntity_1700;
	}
	public void setLoginJSONResultEntity(LoginJSONResultEntity loginJSONResultEntity) {
		this.loginJSONResultEntity_1700 = loginJSONResultEntity;
	}
	
	
	public boolean isUpdateMenuing() {
		return isUpdateMenuing;
	}

	public void setUpdateMenuing(boolean isUpdateMenuing) {
		this.isUpdateMenuing = isUpdateMenuing;
	}

	public boolean isReadChannel() {
		return isReadChannel;
	}

	public void setReadChannel(boolean isReadChannel) {
		this.isReadChannel = isReadChannel;
	}

	public String getChannelID() {
		return channelID;
	}

	public void setChannelID(String channelID) {
		this.channelID = channelID;
	}

	public boolean isHiddenSwitchAccount() {
		return isHiddenSwitchAccount;
	}

	public void setHiddenSwitchAccount(boolean isHiddenSwitchAccount) {
		this.isHiddenSwitchAccount = isHiddenSwitchAccount;
	}
	public boolean isVerify() {
		if (loginJSONResultEntity_1700.isVerify()) {
			return true;
		} else return false;
	}

	/**
	 * 
	 * function: 设置屏幕方向
	 *
	 * @param screen_orientation
	 * 
	 * @ author: aXiBa 2013-3-6 上午9:25:50
	 */
	public void setScreen_orientation(int screen_orientation) {
		this.screen_orientation = screen_orientation;
	}


	public String getPackage_name() {
		return package_name;
	}
	public void setPackage_name(String packageName) {
		package_name = packageName;
	}
	
	
	public boolean isShowLog() {
		return isShowLog;
	}

	public void setShowLog(boolean isShowLog) {
		this.isShowLog = isShowLog;
	}

	/**
	 * 
	 * function: 返回登录用户信息
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-4-1 下午2:32:03
	 */
	public Zs4GameUserInfo getSDKUserInfo(){
		if(Helper.isNotNull(loginJSONResultEntity_1700)) {
			return new Zs4GameUserInfo(loginJSONResultEntity_1700.getUserId(),
					loginJSONResultEntity_1700.getToken(),
					loginJSONResultEntity_1700.getLoginname(), //本来是使用昵称，现在给的是LoginName
					loginJSONResultEntity_1700.getAvatarurl(),
					loginJSONResultEntity_1700.isVerify(),
					loginJSONResultEntity_1700.getUserAge());
		}
		
		return null;
	}
	
	/**
	 * @return the userId
	 */
	public String getUserId() {
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getUserId();
		}
		return "";
	}
//	public String getIsPWD() {
//		if (Helper.isNotNull(loginJSONResultEntity_1700)){
//			return loginJSONResultEntity_1700.getIspwd();
//		}
//		return "";
//	}
	public String getUserDesc() {
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getUserDesc();
		}
		return "";
	}
	public String getUsercode() {
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getUsercode();
		}
		return "";
	}

	public String getUserMobile() {
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getMobile();
		}
		return "";
	}
	/**
	 * @return the userToken
	 */
	public String getLoginKey() {
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getLoginKey();
		}
		return "";
	}
	/**
	 * @return the Token
	 */
	public String getToken() {
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getToken();
		}
		return "";
	}
	/**
	 * 
	 * @return the nickName
	 */
	public String getNickName(){
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getNickName();
		}
		return "";
	}

	public void setNickName(String nickname){
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			loginJSONResultEntity_1700.setNickName(nickname);
		}
	}
	/**
	 *
	 * @return the LoginName
	 */
	public String getLoginName(){
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getLoginname();
		}
		return "";
	}
	
	public String getLoginType(){
		if (Helper.isNotNull(loginJSONResultEntity_1700)){
			return loginJSONResultEntity_1700.getLoginType();
		}
		return "";
	}

	public boolean isGuest(){
		//Helper.isNull(getInstance().getLoginJSONResultEntity().getMobile())  原来是判断返回类型  getInstance().getLoginJSONResultEntity().getLoginType().equals(Extra.SDKGAMESTRS.LOING_GUEST)
		if (getInstance().getLoginJSONResultEntity().getLoginType().equals(Extra.SDKGAMESTRS.LOING_GUEST)){
			return true;
		}else{
			return false;
		}
	}
	
	public void setDebug(boolean isDebug) {
		this.isDebug = isDebug;
	}
	
	public boolean isDebug() {
		return isDebug;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getPlayerId() {
		return playerId;
	}

	public void setPlayerId(String playerId) {
		this.playerId = playerId;
	}

	public String getPlayerName() {
		return playerName;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public String getPlayerLevel() {
		return playerLevel;
	}

	public void setPlayerLevel(String playerLevel) {
		this.playerLevel = playerLevel;
	}

	public String getServId() {
		return servId;
	}

	public void setServId(String servId) {
		this.servId = servId;
	}
	
	public String getServName() {
		return servName;
	}

	public void setServName(String servName) {
		this.servName = servName;
	}

	public String getPayExpandData() {
		return payExpandData;
	}

	public void setPayExpandData(String payExpandData) {
		this.payExpandData = payExpandData;
	}

	public  int getCountKefuMessage() {
		return countKefuMessage;
	}

	public  void setCountKefuMessage(int countKefuMessage) {
		this.countKefuMessage = countKefuMessage;
	}

	public int getLoginFirstTime() {
		return loginFirstTime;
	}

	public void setLoginFirstTime(int loginFirstTime) {
		this.loginFirstTime = loginFirstTime;
	}
//	public static String getUmengKey() {
//		return umengKey;
//	}
//
//	public static void setUmengKey(String umengKey) {
//		Zs4GameData.umengKey = umengKey;
//	}


	public String getChannel() {
		return channel;
	}

	public void setChannel(String channel) {
		this.channel = channel;
	}

	/**
	 * 是否登录
	 * @return
	 */
	public boolean isLogined(){
		return Helper.isNotEmpty(this.getUserId());
	}
	

	/**
	 * 
	 * function: 清空登录信息
	 *
	 * 
	 * @ author: aXiBa 2013-4-1 下午2:25:02
	 */
	public void clearLoginInfo() {
		
		loginJSONResultEntity_1700 = null;
	}
	
	/**
	 * 
	 * function: 保存用户登录信息
	 *
	 * @param entity
	 * @return 是否为切换账号
	 * 
	 * @ author: aXiBa 2013-12-15 下午10:07:05
	 */
	public boolean saveUserLoginInfoForLoginedSuccess(LoginJSONResultEntity entity) {
		
		boolean isSwitchAccount = false;
		boolean isLogined = isLogined();
		if(isLogined) {
			isSwitchAccount = true;
		}
		
		entity.setIsdelete(Extra.BOOLEAN_FALSE);
		entity.setUpdatetime(new Date().getTime());
		
		setLoginJSONResultEntity(entity);
		
		// 保存最后登录信息
		saveLastLoginedUserInfo(entity);
		
		updateUserInfoToStorage(entity, false);
		
		return isSwitchAccount;
	}
	
	
	/**
	 * 
	 * function: 删除账号:只是标记一下，在保存时删除
	 *
	 * @param entity
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:50:26
	 */
	public void deleteAccount(LoginJSONResultEntity entity) {
		
		entity.setIsdelete(Extra.BOOLEAN_TRUE);
		entity.setUpdatetime(new Date().getTime());
		
		updateUserInfoToStorage(entity, true);
		
	}
	
	
	/**
	 * 
	 * function: 更新账号昵称
	 *
	 * @param nickname
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:50:26
	 */
	public void updateAccountNickname(String nickname) {
		
		LoginJSONResultEntity entity = getLoginJSONResultEntity();
		if(Helper.isNotNull(entity)) {
			entity.setNickName(nickname);
			entity.setUpdatetime(new Date().getTime());
			
			saveLastLoginedUserInfo(entity);
			
			updateUserInfoToStorage(entity, false);
		}
		
	}
	
	
	/**
	 * 
	 * function: 获取用户列表, 根据更新时间排序
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:50:38
	 */
	public ArrayList<LoginJSONResultEntity> getUserInfoListOrderForUpdateTime() {
		
		ArrayList<LoginJSONResultEntity> list = getUserInfoList();
		
		// 按updatetime排序
		if(Helper.isNotEmpty(list)) {
			Collections.sort(list, new SortByUpdateTime());
		}
		
		return list;
	}
	
	
	/**
	 * 
	 * function: 获取用户列表
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-30 上午11:31:56
	 */
	public ArrayList<LoginJSONResultEntity> getUserInfoList() {
		
		ArrayList<LoginJSONResultEntity> list = getUserInfoListForStorage();
		
		// 存储
		saveUserListToStorage(list, false);
		
		// 去掉已经删除的
		removeDeleteUserList(list);
		
		return list;
	}
	
	
	/**
	 * 
	 * function: 是否有登录过的用户列表
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-12 下午12:00:20
	 */
	public boolean hasLoginedUserList() {
		
		boolean result = false;
		
		ArrayList<LoginJSONResultEntity> list = getUserInfoListForStorage();
		
		// 去掉已经删除的
		removeDeleteUserList(list);
				
		if(Helper.isNotEmpty(list)) {
			result = true;
		}
		
		return result;
	}

	
	/**
	 * 
	 * function: 更新当前登录用户的邮箱信息
	 *
	 * @param email_new
	 * 
	 * @ author: aXiBa 2013-12-13 下午6:40:19
	 */
	public void updateUserEmail(String email_new) {
		
		LoginJSONResultEntity entity = getLoginJSONResultEntity();
		if(Helper.isNotNull(entity)) {
			entity.setUsercode(email_new);
			entity.setUpdatetime(new Date().getTime());
			entity.setIsAutoAssign(Extra.BOOLEAN_FALSE);
			entity.setFlag_updateEmail(Extra.BOOLEAN_TRUE);
		}
		
		LoginJSONResultEntity entity_last = getLastLoginedUserInfo();
		if(Helper.isNotNull(entity_last)) {
			entity_last.setUsercode(email_new);
			entity_last.setIsAutoAssign(Extra.BOOLEAN_FALSE);
			entity_last.setFlag_updateEmail(Extra.BOOLEAN_TRUE);
			saveLastLoginedUserInfo(entity_last);
		}
		
		updateUserInfoToStorage(entity, false);
	}
	
	/**
	 * 
	 * function: 得到本地用户信息
	 *
	 * @param userid
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-17 上午12:27:34
	 */
	public LoginJSONResultEntity getLocalUserInfo(String userid) {
		
		ArrayList<LoginJSONResultEntity> list = getUserInfoListForStorage();
		if(Helper.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				LoginJSONResultEntity entity = list.get(i);
				if(entity.getUserId().equals(userid)) {
					return entity;
				}
			}
		}
		return null;
	}
	
	
	/**
	 * 
	 * function: 更新当前登录用户的修改密码成功
	 *
	 * @param
	 * 
	 * @ author: aXiBa 2013-12-13 下午6:40:19
	 */
	public void updateUserPassword() {
		
		LoginJSONResultEntity entity = getLoginJSONResultEntity();
		if(Helper.isNotNull(entity)) {
			entity.setUpdatetime(new Date().getTime());
			entity.setIsAutoAssign(Extra.BOOLEAN_FALSE);
			entity.setPassword("");
			entity.setFlag_updatePassword(Extra.BOOLEAN_TRUE);
		}
			
		LoginJSONResultEntity entity_last = getLastLoginedUserInfo();
		if(Helper.isNotNull(entity_last)) {
			entity_last.setIsAutoAssign(Extra.BOOLEAN_FALSE);
			entity_last.setPassword("");
			entity_last.setFlag_updatePassword(Extra.BOOLEAN_TRUE);
			saveLastLoginedUserInfo(entity_last);
		}
		
		updateUserInfoToStorage(entity, false);
	}
	
	/**
	 * 取得单例
	 * @return
	 */
	public static Zs4GameData getInstance(){
		if (Helper.isNull(sInstance)){
			sInstance = new Zs4GameData();
		}
		return sInstance;
	}
	
	/**
	 * 清除单例
	 */
	public static void clearInstance(){
		int appId = 0;
		String appKey = null;
		int appCompany = 0;
		int screen_orientation = Zs4GameBase.SCREEN_ORIENTATION_LANDSCAPE;
		boolean isHiddenSwitchAccount = false;
		boolean isShowLog = false;
		boolean isShowLogoButon = false;
		boolean isMenuLeft = true;
		String channel = null;
		if (sInstance != null){
			appId = sInstance.getAppId();
			appKey = sInstance.getAppKey();
			appCompany = sInstance.getCompany();
			screen_orientation = sInstance.getScreen_orientation();
			isHiddenSwitchAccount = sInstance.isHiddenSwitchAccount();
			isShowLog = sInstance.isShowLog();
			isShowLogoButon = sInstance.isShowLogoButon();
			isMenuLeft = sInstance.isMenuLeft();
			channel = sInstance.getChannel();
		}
		sInstance = null;
		getInstance().setAppId(appId);
		getInstance().setAppKey(appKey);
		getInstance().setCompany(appCompany);
		getInstance().setScreen_orientation(screen_orientation);
		getInstance().setMenuLeft(isMenuLeft);
		
		getInstance().setCanShowWelcomeToast(false);
		getInstance().setHiddenSwitchAccount(isHiddenSwitchAccount);
		getInstance().setShowLogoButon(isShowLogoButon);
		getInstance().setShowLog(isShowLog);
		getInstance().setLoginJSONResultEntity(null);
		getInstance().setChannel(channel);
	}

	/**
	 * 
	 * function: 读取最后登录的用户信息
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-11 下午5:18:36
	 */
	public LoginJSONResultEntity getLastLoginedUserInfo() {
		
		LoginJSONResultEntity entity = null;
		
		String userInfoStr = EncryptHelper.decryptByRC4(PreferencesHelper.getInstance(Extra.Preferences.SDKGAME_PFRFILE_USER_CONFIG)
																			.getString(Extra.Preferences.SDKGAME_KEY_LASTLOGINEDUSERINFO));
		if(Helper.isNotEmpty(userInfoStr)) {
			entity = getLoginEntity(userInfoStr);
		}
		return entity;
	}

	private LoginJSONResultEntity getLoginEntity(String userInfoStr){
		LoginJSONResultEntity entity = null;
		/*
		if(Extra.isComplex()){
		//	entity = new AutoLoginJSONResultEntity(userInfoStr);
		}else{
		//	entity = new LoginJSONResultEntity(userInfoStr);
		}*/
		entity = new AutoLoginJSONResultEntity(userInfoStr);
		return entity;
	}
	private LoginJSONResultEntity getLoginEntity(JSONObject userInfoStr){
		LoginJSONResultEntity entity = null;
		/*
		if(Extra.isComplex()){
			//	entity = new AutoLoginJSONResultEntity(userInfoStr);
		}else{
			//	entity = new LoginJSONResultEntity(userInfoStr);
		}*/
		entity = new AutoLoginJSONResultEntity(userInfoStr);
		return entity;
	}
	/**
	 * 
	 * function: 清空最后一次登录的用户信息
	 *
	 * 
	 * @ author: aXiBa 2013-12-11 下午5:26:30
	 */
	public void clearLastLoginUserInfo() {
		
		PreferencesHelper.getInstance(Extra.Preferences.SDKGAME_PFRFILE_USER_CONFIG)
							.putString(Extra.Preferences.SDKGAME_KEY_LASTLOGINEDUSERINFO, "");
	}
	
	
	/**
	 * 
	 * function: 保存最后一次登录的用户信息
	 *
	 * @param entity
	 * 
	 * @ author: aXiBa 2013-12-11 下午5:33:31
	 */
	public void saveLastLoginedUserInfo(LoginJSONResultEntity entity) {
		PreferencesHelper.getInstance(Extra.Preferences.SDKGAME_PFRFILE_USER_CONFIG)
							.putString(Extra.Preferences.SDKGAME_KEY_LASTLOGINEDUSERINFO, EncryptHelper.encryptByRC4(Helper.isNull(entity) ? "" : entity.toString()));
	}
	
	
	/**
	 * 
	 * function: 更新用户信息到存储
	 *
	 * @param entity
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:51:35
	 */
	private void updateUserInfoToStorage(LoginJSONResultEntity entity, boolean isDelete) {
		
		ArrayList<LoginJSONResultEntity> list = getUserInfoListForStorage();
		if(Helper.isNotEmpty(list)) {
			if(isContainUserAndUpdate(list, entity)) {
				
			}
			else {
				list.add(entity);
			}
		}
		else {
			list.add(entity);
		}
		
		// 存储
		saveUserListToStorage(list, isDelete);
	}
	
	
	/**
	 * 
	 * function: 是否存在,存在更新
	 *
	 * @param list
	 * @param entity
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:51:52
	 */
	private boolean isContainUserAndUpdate(ArrayList<LoginJSONResultEntity> list, LoginJSONResultEntity entity) {
		boolean result = false;
		
		if(Helper.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				LoginJSONResultEntity item = list.get(i);
				if(item.getUserId().equals(entity.getUserId())) {
					if(entity.getUpdatetime() > item.getUpdatetime()) {
						item.setUsercode(entity.getUsercode());
						item.setAvatarurl(entity.getAvatarurl());
						item.setLoginType(entity.getLoginType());
						item.setNickName(entity.getNickName());
						item.setToken(entity.getToken());
						item.setUserDesc(entity.getUserDesc());
						item.setClearPasswordFlg(entity.getClearPasswordFlg());
						item.setUpdatetime(entity.getUpdatetime());
						item.setIsdelete(entity.getIsdelete());
						item.setIsAutoAssign(entity.getIsAutoAssign());
						item.setPassword(entity.getPassword());
						item.setFlag_updatePassword(entity.getFlag_updatePassword());
						item.setFlag_updateEmail(entity.getFlag_updateEmail());
						item.setMobile(entity.getMobile());
						item.setLoginname(entity.getLoginname());
						item.setIspwd(entity.getIspwd());
					}
					result = true;
					break;
				}
			}
		}
		
		return result;
	}
	
	
	/**
	 * 
	 * function: 移动标识isdelete的用户
	 *
	 * @param list
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:52:04
	 */
	private void removeDeleteUserList(ArrayList<LoginJSONResultEntity> list) {
		
		ArrayList<LoginJSONResultEntity> list_move = new ArrayList<LoginJSONResultEntity>();
		
		if(Helper.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				LoginJSONResultEntity curEntity = list.get(i);
				if(Extra.BOOLEAN_TRUE.equals(curEntity.getIsdelete())) {
					list_move.add(curEntity);
				}
			}
			
			if(list_move.size()>0) {
				list.removeAll(list_move);
			}
		}
	}
	
	
	/**
	 * 
	 * function: 获取存储的用户信息
	 *
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:52:26
	 */
	private ArrayList<LoginJSONResultEntity> getUserInfoListForStorage() {
	
		ArrayList<LoginJSONResultEntity> list = new ArrayList<LoginJSONResultEntity>();
		
		ArrayList<LoginJSONResultEntity> list_internalStorage = getUserInfoListForStorage(FileUtils.getInternalFileDir().concat(Extra.FILE_UL));

		HashMap<String, LoginJSONResultEntity> map = new HashMap<String, LoginJSONResultEntity>();
		synUserInfoList(map, list_internalStorage);

//		if (ActivityHelper.checkPermission_WRITE_EXTERNAL_STORAGE()) {
//			ArrayList<LoginJSONResultEntity> list_externalStorage = getUserInfoListForStorage(FileUtils.getSDCardRootDir().concat(Extra.PATH_SD_UL).concat(Extra.FILE_UL));
//			synUserInfoList(map, list_externalStorage);
//		}
		
		Iterator<LoginJSONResultEntity> ite = map.values().iterator();
		while(ite.hasNext()) {
			list.add(ite.next());
		}
		
		return list;
	}
	
	
	/**
	 * 
	 * function: 同步用户列表信息
	 *
	 * @param map
	 * @param list
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:52:46
	 */
	private void synUserInfoList(HashMap<String, LoginJSONResultEntity> map, ArrayList<LoginJSONResultEntity> list) {
		
		if(Helper.isNotEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				LoginJSONResultEntity curEntity = list.get(i);
				String curUserId = curEntity.getUserId();
				if(map.containsKey(curUserId)) {
					LoginJSONResultEntity mapEntity = map.get(curUserId);
					if(curEntity.getUpdatetime()>mapEntity.getUpdatetime()) {
						map.put(curUserId, curEntity);
					}
				}
				else {
					map.put(curUserId, curEntity);
				}
			}
		}
	}
	
	
	
	/**
	 * 
	 * function: 从指定文件中获取用户列表
	 *
	 * @param file
	 * @return
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:53:31
	 */
	private ArrayList<LoginJSONResultEntity> getUserInfoListForStorage(String file) {
		
		ArrayList<LoginJSONResultEntity> list = new ArrayList<LoginJSONResultEntity>();
		
		String userListStr = EncryptHelper.decryptByRC4(FileUtils.readContent(file));
		if(Helper.isNotEmpty(userListStr)) {
			try {
				JSONArray array = new JSONArray(userListStr);
				if(Helper.isNotNull(array) && array.length()>0) {
					for (int i = 0; i < array.length(); i++) {
						LoginJSONResultEntity entity = null;
						entity = getLoginEntity(array.getJSONObject(i));
						list.add(entity);
					}
				}
			} catch (JSONException e) {
				e.printStackTrace();
			} 
		}
		
		return list;
	}
	
	
	/**
	 * 
	 * function: 保存用户信息到存储
	 *
	 * @param list
	 * 
	 * @ author: aXiBa 2013-12-12 上午11:53:54
	 */
	private void saveUserListToStorage(ArrayList<LoginJSONResultEntity> list, boolean isDelete) {
		
		// 删除
		if(isDelete) {
			if(Helper.isNotEmpty(list)) {
				ArrayList<LoginJSONResultEntity> removeList = new ArrayList<LoginJSONResultEntity>();
				for (int i = 0; i < list.size(); i++) {
					LoginJSONResultEntity entity = list.get(i);
					if(Extra.BOOLEAN_TRUE.equals(entity.getIsdelete()) 
							&& !Extra.BOOLEAN_TRUE.equals(entity.getIsAutoAssign())
							&& !Extra.BOOLEAN_FALSE.equals(entity.getFlag_updateEmail())
							&& !Extra.BOOLEAN_FALSE.equals(entity.getFlag_updatePassword())) {
						removeList.add(entity);
					}
				}
				
				if(Helper.isNotEmpty(removeList)) {
					list.removeAll(removeList);
				}
			}
		}
		try {
			if (Helper.isNotEmpty(list)) {
				String content = EncryptHelper.encryptByRC4(list.toString());
				FileUtils.writeContent2File(content, FileUtils.getInternalFileDir(), Extra.FILE_UL);
//				if (ActivityHelper.checkPermission_WRITE_EXTERNAL_STORAGE()) {
//					FileUtils.writeContent2File(content, FileUtils.getSDCardRootDir().concat(Extra.PATH_SD_UL), Extra.FILE_UL);
//				}
			} else {
				FileUtils.writeContent2File("", FileUtils.getInternalFileDir(), Extra.FILE_UL);
//				if (ActivityHelper.checkPermission_WRITE_EXTERNAL_STORAGE()) {
//					FileUtils.writeContent2File("", FileUtils.getSDCardRootDir().concat(Extra.PATH_SD_UL), Extra.FILE_UL);
//				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	
	
	
	/**
	 * 
	 * function: 用户排序:按updatetime
	 * 
	 * @ author: aXiBa
	 */
	class SortByUpdateTime implements Comparator<LoginJSONResultEntity> {

		@Override
		public int compare(LoginJSONResultEntity lhs, LoginJSONResultEntity rhs) {
			
			long updatetime_l = lhs.getUpdatetime();
			long updatetime_r = rhs.getUpdatetime();
			
			if(updatetime_l==updatetime_r) {
				return 0;
			}
			else if(updatetime_l>updatetime_r) {
				return -1;
			}
			else {
				return 1;
			}
		}
		
	}

}
